package com.esyoga.yoyoga.service.impl;
import com.esyoga.yoyoga.mapper.TTrainerMapper;
import com.esyoga.yoyoga.pojo.TStudent;
import com.esyoga.yoyoga.pojo.TStudentExample;
import com.esyoga.yoyoga.pojo.TTrainer;
import com.esyoga.yoyoga.pojo.TTrainerExample;
import com.esyoga.yoyoga.service.TrainerVerifyService;
import lombok.Data;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 教练验证service
 * @author YangTao
 */
@Data
@Service
public class TrainerVerifyServiceImpl implements TrainerVerifyService {
    @Resource
    private TTrainerMapper tTrainerMapper;
    /**
     * 教练注册
     *
     * @param phoneNumber ：手机号
     * @param password
     * @return
     */
    @Override
    public int trainerRegister(String phoneNumber, String password) {
        TTrainer tTrainer = new TTrainer();
        tTrainer.settAccount(phoneNumber);
        tTrainer.settPassword(password);
        tTrainer.settImage("/iamge");
        tTrainer.settFreetime("0,0,0;0,0,0;0,0,0;0,0,0;0,0,0");
        int i = tTrainerMapper.insertSelective(tTrainer);
        return i;
    }

    /**
     * 通过教练手机号查密码
     *
     * @param phone
     * @return
     */
    @Override
    public String findTrainerPasswordByPhone(String phone) {
        String password = null;
        TTrainerExample tTrainerExample = new TTrainerExample();
        //通过Criteria构造查询条件
        TTrainerExample.Criteria criteria = tTrainerExample.createCriteria();
        criteria.andTAccountEqualTo(phone);
        List<TTrainer> list = tTrainerMapper.selectByExample(tTrainerExample);
        if (list.size() != 0) {
            TTrainer tTrainer = list.get(0);
            password = tTrainer.gettPassword();
        }
        return password;

    }

    /**
     * 通过教练手机号查信息
     *
     * @param phone
     * @return
     */
    @Override
    public TTrainer findTrainerByPhone(String phone) {
        TTrainer tTrainer = null;
        TTrainerExample tTrainerExample = new TTrainerExample();
        //通过Criteria构造查询条件
        TTrainerExample.Criteria criteria = tTrainerExample.createCriteria();
        criteria.andTAccountEqualTo(phone);
        List<TTrainer> list = tTrainerMapper.selectByExample(tTrainerExample);
        if (list.size() != 0) {
            tTrainer = list.get(0);
        }
        return tTrainer;
    }

    /**
     * 完善用户信息
     *
     * @param tTrainer
     * @return
     */
    @Override
    public int trainerAddInfo(TTrainer tTrainer) {
        TTrainerExample tTrainerExample = new TTrainerExample();
        TTrainerExample.Criteria criteria = tTrainerExample.createCriteria();
        criteria.andTAccountEqualTo(tTrainer.gettAccount());
        int i = tTrainerMapper.updateByExampleSelective(tTrainer, tTrainerExample);
        return i;
    }

    /**
     * 用户重置密码
     *
     * @param restPhone
     * @param restPassword
     * @return
     */
    @Override
    public int resetPassword(String restPhone, String restPassword) {
        TTrainerExample tTrainerExample = new TTrainerExample();
        TTrainerExample.Criteria criteria = tTrainerExample.createCriteria();
        criteria.andTAccountEqualTo(restPhone);
        TTrainer tTrainer = new TTrainer();
        tTrainer.settAccount(restPhone);
        tTrainer.settPassword(restPassword);
        int i = tTrainerMapper.updateByExampleSelective(tTrainer, tTrainerExample);
        return i;
    }

}
